package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarParkingAccelerator;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.config.ConfigMap;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.math.BigDecimal;

/**
 * 停车轰油门算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计<br/>
 */
public class ParkingAccelerator extends AbstractArithmetic<VarParkingAccelerator> {
    /**
     * 发动机怠速判定阈值
     */
    private Integer rotationThreshold = ConfigMap.getInteger("ParkingStepOnAccelerator_rotation_threshold");
    /**
     * 判断当前点是否是停车轰油门
     *
     * @param speedKmh          当前速度
     * @param rotation          当前转速
     * @param accelerator       当前油门开度
     * @param rotationThreshold 怠速转速阈值
     * @return
     */
    private boolean isParkingStepOnAccelerator(int speedKmh, int rotation, double accelerator, Integer rotationThreshold) {
        return speedKmh==0 && accelerator>0 && rotation>rotationThreshold;
    }

    @Override
    public void calculate() {
        logger.debug("===============停车轰油门算法开始===========");
        VarParkingAccelerator var = super.getObj();
        CommonData commonData = getCommonData();

        int speedKmh = commonData.getSpeedKmh();
        int rotation = commonData.getRotation();
        double accelerator = commonData.getAcceleratorPosition();
        logger.debug("当前转速：{},当前点速度(单位:KM/H)：{},当前油门开度：{},发动机怠速判定阈值：{}",rotation, speedKmh, accelerator, rotationThreshold);

        //判断当前点是否是停车轰油门
        boolean curState = isParkingStepOnAccelerator(speedKmh, rotation, accelerator, rotationThreshold);

        //上一点是停车轰油门，不管当天点是不是停车轰油门，统计数据
        if (var.isPreParkingAccelerator()) {
            //停车轰油门时间(单位:秒)
            double time = var.getDuration();
            //与上个点的时间差
            BigDecimal dffTimeSecond = BigDecimal.valueOf(commonData.getDffTime()).divide(new BigDecimal("1000"), 2, BigDecimal.ROUND_HALF_UP);

            //累加停车轰油门时长
            double accTime = dffTimeSecond.add(BigDecimal.valueOf(time)).doubleValue();
            var.setDuration(accTime);
            logger.debug("两点时间差(单位:秒)：{},累加前停车轰油门时间(单位:秒)：{},累加后停车轰油门时间(单位:秒)：{}",dffTimeSecond, time, accTime);

            //累加停车轰油门油耗
            //停车轰油门油耗
            double dffOil = commonData.getDffFuel();
            double fuel = var.getFuel();
            double accFuelConsumption = BigDecimal.valueOf(fuel).add(BigDecimal.valueOf(dffOil)).doubleValue();
            var.setFuel(accFuelConsumption);
            logger.debug("油耗(单位:ml)：{},累加前停车轰油门油耗(单位:ml)：{},累加后停车轰油门油耗(单位:ml)：{}",dffOil, fuel, accFuelConsumption);
        }

        //更新状态，供下个点使用
        var.setPreParkingAccelerator(curState);
        logger.debug("===============停车轰油门算法结束===========");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarParkingAccelerator var = getObj();
        statisticDataOut.setParkThrottleFuel(ArithmeticUtils.fuelMlToMl(var.getFuel()));
        statisticDataOut.setParkThrottleDuration(ArithmeticUtils.timeSToS(var.getDuration()));
    }
}
